<template>
  <div class="page-wrapper">
    <PageTools>
      <template #after>
        <el-button type="primary" size="small" @click="showAddDialog(1, '0')">新增页面菜单权限</el-button>
      </template>
    </PageTools>
    <el-card>
      <!--
        树形表格必须以字符串形式传入row-key
        指定每个对象中那个字段作为唯一标识
        default-expand-all 可以控制进入页面时自动展开所有节点
      -->
      <el-table :data="list" border row-key="id" default-expand-all>
        <el-table-column label="名称" prop="name" />
        <el-table-column label="标识" prop="code" />
        <el-table-column label="描述" prop="description" />
        <el-table-column label="操作">
          <template v-slot="scope">
            <el-button v-if="scope.row.type===1" type="text" @click="showAddDialog(2, scope.row.id)">新增功能点子权限</el-button>
            <el-button type="text" @click="showEditDialog(scope.row.id)">编辑</el-button>
            <el-button type="text" @click="delPerm(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <el-dialog title="编辑权限" :visible="isShowDialog" @close="btnCancel">
      <el-form ref="addForm" :model="formData" :rules="rules">
        <el-form-item label="权限名称" prop="name">
          <el-input v-model="formData.name" />
        </el-form-item>
        <el-form-item label="权限标识" prop="code">
          <el-input v-model="formData.code" />
        </el-form-item>
        <el-form-item label="权限描述" prop="description">
          <el-input v-model="formData.description" />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button size="small" type="primary" :disabled="loading" @click="btnOK">确定</el-button>
        <el-button size="small" @click="btnCancel">取消</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { getPermList, getPermissionDetail, updatePermission, addPermission, delPermission } from '@/api/permission'
import { listToTree } from '@/utils'
export default {
  data() {
    return {
      loading: false,
      // 数据存放
      list: [],
      isShowDialog: false,
      formData: {
        name: '',
        code: '',
        description: ''
      },
      rules: {
        name: [
          { required: true, message: '该项不能为空', trigger: 'blur' }
        ],
        code: [
          { required: true, message: '该项不能为空', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '该项不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    // 调用加载函数
    this.loadList()
  },
  methods: {
    async delPerm(id) {
      try {
        // 二次询问
        await this.$confirm('是否确认删除')
        // 发请求
        await delPermission(id)
        // 提醒用户
        this.$message.success('操作成功')
        // 更新页面
        this.loadList()
      } catch (error) {
        console.log(error)
      }
    },
    showAddDialog(type, pid) {
      this.formData.type = type
      this.formData.pid = pid
      this.isShowDialog = true
    },
    async showEditDialog(id) {
      console.log('作用域插槽获取到的数据', id)
      this.formData = await getPermissionDetail(id)
      this.isShowDialog = true
    },
    async loadList() {
      // 发请求拿数据
      const res = await getPermList()
      // console.log('获取源数据', res)
      // 权限数据是有层级关系的, 需要利用之前的那个递归函数将他转换为 children 的嵌套
      // 才能当做树形显示
      this.list = listToTree(res, '0')
      console.log('转换后新数据', this.list)
    },
    async btnOK() {
      // 等待校验
      await this.$refs.addForm.validate()
      this.loading = true
      // 发请求
      // 通过表单 formData 是否拥有 id 决定是新建还是编辑
      if (this.formData.id) {
        // 编辑
        await updatePermission(this.formData)
      } else {
        // 新增
        await addPermission(this.formData)
      }
      // 提示用户
      this.$message.success('操作成功')
      // 关闭弹窗
      this.isShowDialog = false
      // 更新页面
      this.loadList()
      this.loading = false
    },
    btnCancel() {
      // 清理报错
      this.$refs.addForm.resetFields()
      // 清理数据
      this.formData = {
        name: '',
        code: '',
        description: ''
      }

      // 关闭弹窗
      this.isShowDialog = false
    }
  }
}
</script>

<style>

</style>
